/**
 * Created by dengyuntao on 2017/8/8.
 */
var app = angular.module('appointApp', []);
app.controller('appointCtrl', function ($scope, $http) {
    //后台获取原始的汽服数据接口
    $http({
        method: 'GET',
        url: 'data.php'      //这里是你的后台数据接口
    }).then(function successCallback(response) {
        $scope.list = response.data.list;  //后台获取的数据可以替换下面自定义的数据
    }, function errorCallback(response) {
        // 请求失败执行代码
    });

    //后台获取订单数据接口
    $http({
        method: 'GET',
        url: 'orderResult.php'      //这里是你的后台数据接口
    }).then(function successCallback(response) {
        $scope.orderList = response.data.orderList; //后台获取的数据可以替换下面自定义的数据
    }, function errorCallback(response) {
        // 请求失败执行代码
    });


    $scope.list = [
        [{image: "img/1.jpg", item: "全车打蜡", price: "80"}],
        [{image: "img/1.jpg", item: "加装夏季防虫网", price: "80"}, {image: "img/1.jpg", item: "油改气", price: "880"}],
        [{image: "img/1.jpg", item: "车身油漆修补", price: "80"}, {image: "img/1.jpg", item: "音箱", price: "880"}],
        [{image: "img/1.jpg", item: "精品洗车", price: "80"}],
        [{image: "img/1.jpg", item: "四轮定位", price: "80"}]

    ];

    $scope.sumPrice = 0;        //预约的总价
    $scope.sumNum = 0;             //预约的项目数
    $scope.orderList = [];
    $scope.sum = function (x) {           //总价的计算
        x.item_selected = !x.item_selected;
        console.log(x.item_selected);
        if (x.item_selected) {
            $scope.sumPrice = parseInt($scope.sumPrice) + parseInt(x.price);
            $scope.sumNum = $scope.sumNum + 1;
            $scope.orderList.push({image: x.image, item: x.item, price: x.price});
        }
        else {
            $scope.sumPrice = parseInt($scope.sumPrice) - parseInt(x.price);
            $scope.sumNum = $scope.sumNum - 1;
            $scope.orderList.pop({image: x.image, item: x.item, price: x.price});
        }
    };

    $("#buy").click(function () {
        var information = [];
        var remarks = [];
        var phone = '';
        var date = '';
        console.log($('.phone').val());
        $(".phone").each(function () {
            phone = $(this).val();
        });
        $(".day").each(function () {
            date = $(this).val();
        });
        $(".remarks").each(function () {
            remarks.push($(this).val());
        });
        if (phone == '') {
            alert("请输入电话号码");
        } else if (date == '') {
            alert("请选择预约时间");
        } else {

            for (var i = 0; i < $scope.orderList.length; i++) {
                information.push({
                    'item': $scope.orderList[i].item,
                    'time': date,
                    'price': $scope.orderList[i].price,
                    'phone': phone,
                    'remarks': remarks[i]
                });
            }

            var btn_target = $(this);

            btn_target.addClass("disabled");

            var data = {
                information: information
            };

            $http({
                method: 'POST',
                url: '/customer/yuyue/index',
                data: data
            }).then(function successCallback(response) {
                //alert(response.msg);
                btn_target.removeClass("disabled");
                window.location.href = "/customer/yuyue/index";

            }, function errorCallback(response) {
                // 请求失败执行代码
                window.location.href = "/customer/yuyue/index";
            });
        }

    });
});
